package com.aier.admin_new.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.school.entity.School;

public class SchoolRowMapper implements RowMapper<School> {

    private int type;
    
    public static final int AND_MANAGERS = 1;
    
    public static final int AND_USERNUMS = 2;
    
    public static final int AND_PARENTNUMS = 3;
    
    public static final int AND_MESSAGE = 4;
    
    public static final int AND_BUY = 5;
    
    public SchoolRowMapper() {
    }

    public SchoolRowMapper(int type) {
        this.type = type;
    }

    @Override
    public School mapRow(ResultSet rs, int rowNum) throws SQLException {
    	School s = parseSchool(new School(), rs);
    	switch (type) {
            case AND_MANAGERS:
            	s.setManagers(rs.getString("managers"));
                break;
            case AND_USERNUMS:
                s.setNum_teacher(rs.getInt("num_teacher"));
                s.setNum_student(rs.getInt("num_student"));
                s.setNum_parent(rs.getInt("num_parent"));
                break;
            case AND_PARENTNUMS:
                s.setNum_parent(rs.getInt("num_parent"));
                break;
            case AND_MESSAGE:
                s.setCount(rs.getInt("count"));
                break;
            case AND_BUY:
                s.setNum_parent(rs.getInt("num_parent"));
                if (rs.getInt("method") == 1 || rs.getInt("method") == 0) {
                    s.setMethodStr("月付费用户");
                } else {
                    s.setMethodStr("购买短信用户");
                }
                break;
    	}
        return s;
    }

    public static School parseSchool(School s, ResultSet rs) throws SQLException {
        return parseSchool(s, rs, null);
    }

    public static School parseSchool(School s, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            s.setSid(rs.getInt(su+"sid"));
        } catch (SQLException e) {
            s.setSid(null);
        }
        try {
            s.setName(rs.getString(su+"name"));
        } catch (SQLException e) {
            s.setName(null);
        }
        try {
            s.setPid(rs.getInt(su+"pid"));
        } catch (SQLException e) {
            s.setPid(null);
        }
        try {
            s.setCid(rs.getInt(su+"cid"));
        } catch (SQLException e) {
            s.setCid(null);
        }
        try {
            s.setDesinfo(rs.getString(su+"desinfo"));
        } catch (SQLException e) {
            s.setDesinfo(null);
        }
        try {
            s.setImg(rs.getString(su+"img"));
        } catch (SQLException e) {
            s.setImg(null);
        }
        try {
            s.setSlogo(rs.getString(su+"slogo"));
        } catch (SQLException e) {
            s.setSlogo(null);
        }
        try {
            s.setMesCount(rs.getInt(su+"mesCount"));
        } catch (SQLException e) {
            s.setMesCount(null);
        }
        try {
            s.setMethod(rs.getInt(su+"method"));
        } catch (SQLException e) {
            s.setMethod(null);
        }
        try {
            s.setClassCount(rs.getInt(su+"classCount"));
        } catch (SQLException e) {
            s.setClassCount(null);
        }
        try {
            s.setMesSuffix(rs.getString(su+"mesSuffix"));
        } catch (SQLException e) {
            s.setMesSuffix(null);
        }
        return s;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, School s) {
        par.put("sid", s.getSid());
        par.put("name",s.getName());
        par.put("cid",s.getCid());
        par.put("pid", s.getPid());
        par.put("desinfo",s.getDesinfo());
        par.put("img",s.getImg());
        par.put("slogo",s.getSlogo());
        par.put("mesCount", s.getMesCount());
        par.put("method", s.getMethod());
        par.put("classCount", s.getClassCount());
        par.put("mesSuffix", s.getMesSuffix());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, School s) {
        args.put("sid",s.getSid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final School s) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(s),
                        new String[] { "sid" });
                parsePS(ps, s);
                return ps;
            }

        };
    }

    private static String parseSQL(School s) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into school(");
        int count = 0;
        if (null != s.getName()) {
            sql.append("name,");
            count++;
        }

        if (null !=s.getPid()) {
            sql.append("pid,");
            count++;
        }
        if (null !=s.getCid()) {
            sql.append("cid,");
            count++;
        }
        if (null != s.getDesinfo()) {
            sql.append("desinfo,");
            count++;
        }
        if (null != s.getImg()) {
            sql.append("img,");
            count++;
        }
        if (null != s.getSlogo()) {
            sql.append("slogo,");
            count++;
        }
        if (null !=s.getMesCount()) {
            sql.append("mesCount,");
            count++;
        }
        if (null !=s.getMethod()) {
            sql.append("method,");
            count++;
        }
        if (null !=s.getMesSuffix()) {
            sql.append("mesSuffix,");
            count++;
        }

        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, School s)
        throws SQLException {
        int count = 1;
        if (null != s.getName()) {
            ps.setString(count++,s.getName());
        }
        if (null != s.getPid()) {
            ps.setInt(count++, s.getPid());
        }
        if (null != s.getCid()) {
            ps.setInt(count++, s.getCid());
        }
        if (null != s.getDesinfo()) {
            ps.setString(count++,s.getDesinfo());
        }
        if (null != s.getImg()) {
            ps.setString(count++,s.getImg());
        }
        if (null != s.getSlogo()) {
            ps.setString(count++,s.getSlogo());
        }
        if (null != s.getMesCount()) {
            ps.setInt(count++, s.getMesCount());
        }
        if (null != s.getMethod()) {
            ps.setInt(count++, s.getMethod());
        }
        if (null != s.getMesSuffix()) {
            ps.setString(count++,s.getMesSuffix());
        }
    }

}
